﻿namespace FR.SAFTAnalyser.ViewModels.Pane
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AvalonDock.Layout;
    using System.Windows.Controls;
    using System.Windows;

    public class LayoutInitializer : ILayoutUpdateStrategy
    {
        public bool BeforeInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableToShow, ILayoutContainer destinationContainer)
        {
            if (anchorableToShow.Content is ITool)
            {
                PaneLocation paneLocation = ((ITool)anchorableToShow.Content).PreferredLocation;

                var toolsPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == paneLocation.Location);

                if (toolsPane == null)
                {
                    switch (paneLocation.Location)
                    {
                        case PaneLocation.LEFT:
                            {
                                //TODO: this should use two steps: first, try to add to existing "LayoutAnchorablePane" if not create layoutAnchorGroup like below
                                var layoutAnchorSide = layout.Descendents().OfType<LayoutAnchorSide>().First(side => side.Side == AnchorSide.Left);
                                var layoutAnchorGroup = new LayoutAnchorGroup();
                                //layoutAnchorGroup.Root.RootPanel.DockWidth;
                                layoutAnchorGroup.InsertChildAt(0, anchorableToShow);
                                layoutAnchorSide.InsertChildAt(0, layoutAnchorGroup);
                                anchorableToShow.AutoHideWidth = 200;
                                break;
                            }
                        case PaneLocation.RIGHT:
                            {
                                var parent = layout.Descendents().OfType<LayoutPanel>().First(d => d.Orientation == Orientation.Horizontal);
                                toolsPane = new LayoutAnchorablePane { DockWidth = new GridLength(200, GridUnitType.Pixel) };
                                parent.Children.Add(toolsPane);

                                break;
                            }
                        case PaneLocation.BOTTOM:
                            {
                                var parent = layout.Descendents().OfType<LayoutPanel>().First(d => d.Orientation == Orientation.Vertical);
                                toolsPane = new LayoutAnchorablePane { DockHeight = new GridLength(200, GridUnitType.Pixel) };
                                parent.Children.Add(toolsPane);
                                anchorableToShow.AutoHideHeight = 400;
                                break;
                            }
                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
                if (toolsPane != null)
                    toolsPane.Children.Add(anchorableToShow);

                bool isAutoHidden = ((ITool)anchorableToShow.Content).IsAutoHidden;

                if (anchorableToShow.IsAutoHidden != isAutoHidden)
                    anchorableToShow.ToggleAutoHide();

                return true;
            }

            return false;
        }

        public void AfterInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableShown)
        {
        }


        public bool BeforeInsertDocument(LayoutRoot layout, LayoutDocument anchorableToShow, ILayoutContainer destinationContainer)
        {
            return false;
        }

        public void AfterInsertDocument(LayoutRoot layout, LayoutDocument anchorableShown)
        {

        }
    }
}
